import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_cost/app/config/route_jump.dart';
import 'package:get/get.dart';

import 'logic.dart';

class HomePage extends StatelessWidget {
  final logic = Get.find<HomeLogic>();
  final state = Get.find<HomeLogic>().state;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: false,
        title: const Text("日常账本"),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [
            _buildBalanceWidget(),
            _buildBudgetWidget(),
            _buildRecordWidget(),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          jump2AddBill();
        },
        backgroundColor: Colors.blueAccent,
        child: const Icon(Icons.add, color: Colors.white),
      ),
    );
  }

  Widget _buildBalanceWidget() {
    return Card(
      margin: const EdgeInsets.all(10),
      child: Container(
        padding: const EdgeInsets.all(15),
        child: Column(
          children: [
            _buildLedgerInfoWidget(),
            _buildLedgerInfo2Widget(),
          ],
        ),
      ),
    );
  }

  Widget _buildLedgerInfoWidget() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: const [
            Text("本月结余", style: TextStyle(fontSize: 14)),
            Text("日常账本"),
          ],
        ),
        const Text(
          "¥0.00",
          style: TextStyle(
            fontSize: 24,
            fontWeight: FontWeight.w600,
          ),
        ),
      ],
    );
  }

  Widget _buildLedgerInfo2Widget() {
    return Padding(
      padding: const EdgeInsets.only(top: 50),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          _buildCurrentExpenditureWidget(),
          _buildCurrentRevenueWidget(),
        ],
      ),
    );
  }

  Widget _buildCurrentExpenditureWidget() {
    return Column(
      children: const [
        Text("本月支出"),
        Text("¥0.00"),
      ],
    );
  }

  // Current month
  Widget _buildCurrentRevenueWidget() {
    return Column(
      children: const [
        Text("本月收入"),
        Text("¥0.00"),
      ],
    );
  }

  Widget _buildBudgetWidget() {
    return Card(
      margin: const EdgeInsets.fromLTRB(10, 0, 10, 10),
      child: Container(
        padding: const EdgeInsets.all(15),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: const [
            Text("剩余"),
            Text("月总预算"),
            Text("剩余日均"),
          ],
        ),
      ),
    );
  }

  Widget _buildRecordWidget() {
    return ListView.builder(
      itemCount: 10,
      shrinkWrap: true,
      physics: const NeverScrollableScrollPhysics(),
      itemBuilder: (context, index) {
        return Card(
          clipBehavior: Clip.antiAlias,
          margin: const EdgeInsets.fromLTRB(10, 0, 10, 10),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              const Padding(
                padding: EdgeInsets.fromLTRB(15, 10, 15, 0),
                child: Text("2023-10-24"),
              ),
              _buildDateRecordWidget(),
            ],
          ),
        );
      },
    );
  }

  Widget _buildDateRecordWidget() {
    return ListView.builder(
      itemCount: Random().nextInt(10) + 1,
      shrinkWrap: true,
      itemExtent: 50,
      physics: const NeverScrollableScrollPhysics(),
      itemBuilder: (context, index) {
        return _buildRecordItemWidget();
      },
    );
  }

  Widget _buildRecordItemWidget() {
    return InkWell(
      onTap: () {
        jump2BillDetail();
      },
      child: SizedBox(
        height: 50,
        child: Row(
          children: [
            _buildItemIconWidget(),
            _buildItemContentWidget(),
            _buildItemRightWidget(),
          ],
        ),
      ),
    );
  }

  Widget _buildItemIconWidget() {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 15),
      child: Image.asset(
        "assets/images/logo.png",
        width: 30,
        height: 30,
      ),
    );
  }

  Widget _buildItemContentWidget() {
    return Expanded(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: const [
          Text("居家生活", style: TextStyle(fontSize: 14)),
          Text(
            "早餐",
            style: TextStyle(
              fontSize: 12,
              color: Color(0xFF666666),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildItemRightWidget() {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 15),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.end,
        children: const [
          Text("-20.00", style: TextStyle(fontSize: 14)),
          Text("招商银行", style: TextStyle(fontSize: 12)),
        ],
      ),
    );
  }
}
